#include "pq.h"

/*
#define LEN	16	
#define HS	7

extern int heapsize;

int main(int argc, char *argv[])
{
    int i, max, key;
    int a[LEN], b[HS];

    srand(time(NULL));
    for (i = 0; i < HS; i++)
    {
	a[i] = rand() % 100;
	b[i] = a[i];
    }
    print_a(a, HS);

    if (build_max_pq(a, HS, LEN) < 0)
    {
	fprintf(stderr, "build_max_pq failed!\n");
	exit(1);
    }
    print_a(a, heapsize);
    //
    //for (i = heapsize; i > 0; i--)
    //{
    //    extract_max(a, &max);
    //    fprintf(stdout, "max:%d\n", max);
    //    print_a(a, heapsize);
    //}
    //
    // 
    // do {
    //     key = rand() % 100;
    //     fprintf(stdout, "key(%d)\n", key);
    // } while (insert(a, LEN, key) >= 0 && (print_a(a, heapsize), 1));
    // 

    heap_sort(a);
    bubble_sort(b, HS);
    print_a(a, HS);
    print_a(b, HS);
    
    return 0;
}
*/

int main(int argc, char *argv[])
{
#if 0
    int size, key, max, todo, todel, iLoop, iUseRand;
    PQ pq;

    switch (argc)
    {
	case 2:
	    iLoop = atoi(argv[1]);
        size = ARRAY_LEN;
	    break;
    case 3:
	    iLoop = atoi(argv[1]);
        size = atoi(argv[2]);
        break;
	default:
        iLoop = 16;
	    size = ARRAY_LEN;
	    break;
    }

    if (init_max_pq(&pq, size) < 0)
    {
	fprintf(stderr, "init failed");
	exit(1);
    }

    srand(time(NULL));
    iUseRand = 1;
    while (iLoop-- > 0)
    {
        if (iUseRand)
        {
            todo = rand() % 10;
        }
        else
        {
            iUseRand = 1;
        }
        fprintf(stdout, "todo:%d\n", todo);
        if (todo == 0)
        {
            /* delete */
            todel = rand() % pq.count;
            fprintf(stdout, "to del a[%d] %d\n", todel, pq.a[todel]);
            if (pq_del(&pq, todel) < 0)
            {
                fprintf(stderr, "del a[%d] %d failed!\n", todel, pq.a[todel]);
                todo = 2;
                iUseRand = 0;
                continue;
            }
        }
        else if (todo == 1)
        {
            /* extract max */
            if (extract_max(&pq, &max) < 0)
            {
                fprintf(stdout, "extract maximum failed.\n");
                todo = 2;
                iUseRand = 0;
                continue;
            }
            else
            {
                fprintf(stdout, "extracted max %d\n", max);
            }
        }
        else
        {
            // insert 
            key = rand() % MY_RAND_MAX;
            if (insert(&pq, key) < 0)
            {
                fprintf(stderr, "insert key %d failed.\n", key);
                todo = 0;
                iUseRand = 0;
                continue;
            }
            else
            {
                fprintf(stdout, "insert key %d\n", key);
            }
        }
        print_a(pq.a, pq.count, NULL);
    }

    finalize_max_pq(&pq);
    exit(0);
#else
    PQ pq;
    int n;
    //int a[] = {5, 3, 17, 10, 84, 19, 6, 22, 9};
    int a[] = {1,2,3};
    n = sizeof(a) / sizeof(a[0]);

    if (init_max_pq(&pq, n) != 0)
    {
        exit(1);
    }

    print_a(a, n, NULL);

    if (build_max_pq(&pq, a, n) != 0)
    {
        exit(1);
    }

    print_a(pq.a, pq.count, NULL);

    exit(0);
#endif
}
